<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'center',border:false">
		<table style="margin-left: 20px;margin-top: 20px;">
				<tr>
					<td>设备列表:</td>
					<td>
						<@eu.comboBox id="deviceName" url="device.do?loadDevices" name="deviceName" required="true" onSelect="selectType()"/>
						<br />
						执行命令:
					</td>
					<td>控制类型:</td>
					<td>
						<@eu.comboBox id="deviceLinkLtype" url="common.do?loadComboBox&key=RCU_CONTROL_TYPE" name="ltype" required="true" onSelect="selectType()"/>
						<br / >
						读取状态命令:
					</td>
					<td>
					</td>
					<td>
						<br / >
						读取状态结果：
					</td>
				</tr>
				
				<tr>
					<td></td>
					<td>
						<textarea id="cmd" style="height: 300px;width:350px" rows="6"  name="cmd" class="easyui-validatebox" ></textarea>
					</td>
					<td></td>
					<td>
						<textarea id="cmdRead" style="height: 300px;width:350px" rows="6"  name="cmdRead" class="easyui-validatebox" ></textarea>
					</td>
					<td>
						<a href="javascript:void(0)" class="easyui-linkbutton" style="background-color: #c5dbf6;" plain="true" onclick="testReadCmd()">状态读取>></a>
					</td>
					<td>
						<textarea id="cmdReadResult" style="height: 300px;width:350px" rows="6"  name="cmdReadResult" class="easyui-validatebox" ></textarea>
					</td>
				</tr>
				<tr>
					<td>
					</td>
					<td>
						<a href="javascript:void(0)" class="easyui-linkbutton" style="background-color: #c5dbf6;" plain="true" onclick="testCmd()">发送命令>></a>
					</td>
				</tr>	
		</table>
	</div>
</div>
<script type="text/javascript">
	function testCmd() {
		
		onLoadingHandler(function() {
			$.post("devOperation.do?sendCmd",{id:$('#deviceName').combobox('getValue'),cmd:$('#cmd').val()}, function(result) {
				var win  = openMsg({msg:result.msg,timeout:5000});
				printLog(win);
				removeload();
	     	}, "JSON");
		});
	}
	
	function testReadCmd() {
		onLoadingHandler(function() {
			$.post("devOperation.do?sendReadStatusCmd",{id:$('#deviceName').combobox('getValue'),cmd:$('#cmdRead').val()}, function(result) {
				showMsg(result.msg);
				$('#cmdReadResult').val(result.msg);
				removeload();
	     	}, "JSON");
		});
		
	}
	
	function selectType() {
		printLog($('#deviceName').combobox('getValue'));
		printLog($('#deviceLinkLtype').combobox('getValue'));
		var deviceId = $('#deviceName').combobox('getValue');
		var operationType = $('#deviceLinkLtype').combobox('getValue');
		if(deviceId==''||operationType=='') {
			showMsg('请选择设备和类型 .');
			return;
		}
		
		$.post("devOperation.do?getOperationTemp",{id:deviceId,type:operationType}, function(result) {
			formatJson(result.obj,"cmd");
     	}, "JSON");
		
		//状态读取命令
		$.post("devOperation.do?getOperationStatus",{id:deviceId,type:operationType}, function(result) {
			formatJson(result.obj,"cmdRead");
     	}, "JSON");
	}

    function repeat(s, count) {
        return new Array(count + 1).join(s);
    }
 
    function formatJson(jsonValue,id) {
 
        var json =  jsonValue;
 
        var i           = 0,
            len          = 0,
            tab         = "    ",
            targetJson     = "",
            indentLevel = 0,
            inString    = false,
            currentChar = null;
             
             
        for (i = 0, len = json.length; i < len; i += 1) { 
            currentChar = json.charAt(i);
 
            switch (currentChar) {
            case '{': 
            case '[': 
                if (!inString) { 
                    targetJson += currentChar + "\n" + repeat(tab, indentLevel + 1);
                    indentLevel += 1; 
                } else { 
                    targetJson += currentChar; 
                }
                break; 
            case '}': 
            case ']': 
                if (!inString) { 
                    indentLevel -= 1; 
                    targetJson += "\n" + repeat(tab, indentLevel) + currentChar; 
                } else { 
                    targetJson += currentChar; 
                } 
                break; 
            case ',': 
                if (!inString) { 
                    targetJson += ",\n" + repeat(tab, indentLevel); 
                } else { 
                    targetJson += currentChar; 
                } 
                break; 
            case ':': 
                if (!inString) { 
                    targetJson += ": "; 
                } else { 
                    targetJson += currentChar; 
                } 
                break; 
            case ' ':
            case "\n":
            case "\t":
                if (inString) {
                    targetJson += currentChar;
                }
                break;
            case '"': 
                if (i > 0 && json.charAt(i - 1) !== '\\') {
                    inString = !inString; 
                }
                targetJson += currentChar; 
                break;
            default: 
                targetJson += currentChar; 
                break;                    
            } 
        } 
        $('#'+id).val(targetJson);
        return;
    }
  </script>